package com.happysmile.tcpconfig.model;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.happysmile.tcpconfig.mybatis.entity.Config;
import com.happysmile.tcpconfig.mybatis.entity.Tcpconfig;
import com.happysmile.tcpconfig.mybatis.mapper.TcpconfigMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.annotation.Resource;
import java.lang.reflect.Field;

@NoArgsConstructor
@Data
@AllArgsConstructor
public class TcpconfigAddDto {

    @Resource
    private TcpconfigMapper tcpconfigMapper;

    public TcpconfigAddDto init(){

        TcpconfigAddDto tcpconfigAddDto = new TcpconfigAddDto();
        //再根据config去tcpconfig找。
        Tcpconfig tcpconfig = tcpconfigMapper.selectOne(new QueryWrapper<Tcpconfig>().eq("cid", 1));//因为仅仅可能插入一条数据。因为这条是系统默认的。
        //取出默认配置
        String params = tcpconfig.getParams();
        String[] split = params.split("-");
        for (String s : split) {
            //拿到参数
            String trim = s.trim();
            //反射遍历,封装对象
            try{
                Class<? extends TcpconfigAddDto> aClass = tcpconfigAddDto.getClass();
                Field[] declaredFields = aClass.getDeclaredFields();
                for (Field declaredField : declaredFields) {
                    String name = declaredField.getName();
                    Object value = declaredField.get(aClass);
                    //如果取到你了，那么把你的值赋值为true
                    if (name.equals(trim)){
                        declaredField.set(value,true);
                    }
                }
            }catch (Exception e){}
            //反射结束
        }
        return tcpconfigAddDto;
    }


    private Boolean A;
    private Boolean c;
    private Boolean C;
    private Boolean d;
    private Boolean dd;
    private Boolean ddd;
    private Boolean D;
    private Boolean e;
    private Boolean E;
    private Boolean f;
    private Boolean F;
    private Boolean i;
    private Boolean l;
    private Boolean L;
    private Boolean m;
    private Boolean M;
    private Boolean b;
    private Boolean n;
    private Boolean nn;
    private Boolean N;
    private Boolean O;
    private Boolean P;
    private Boolean q;
    private Boolean r;
    private Boolean S;
    private Boolean s;
    private Boolean T;
    private Boolean t;
    private Boolean tt;
    private Boolean ttt;
    private Boolean tttt;
    private Boolean u;
    private Boolean v;
    private Boolean vv;
    private Boolean w;
    /*
    * 返回TcpConfig 对象
    * */
    public String toString(){
        StringBuilder sb = new StringBuilder();
         try{
             Class<?> aClass = Class.forName("com.happysmile.tcpconfig.model.TcpconfigAddDto");
             Object o = aClass.newInstance();
             Field[] fields = aClass.getDeclaredFields();
             for (Field field : fields) {
                 String filedName = field.getName();
                 Boolean filedValue = (Boolean) field.get(o);
                 if (filedValue == false){
                     //不拼接
                 }else {
                     //拼接
                     sb.append(" -").append(filedName).append(" ");
                 }
             }
             }catch(Exception e){}
        return sb.toString();
    }

}
